#nullable enable
using System;
using System.Collections.Generic;

namespace ERP.Api.Models
{
    /// <summary>
    /// 新增 BOM 请求 DTO（包含基础信息、配件、工艺路线、附件）
    /// </summary>
    public class BomCreateDto
    {
        /// <summary>BOM 编码</summary>
        public string? BomCode { get; set; }
        /// <summary>系统编码</summary>
        public string? SystemCode { get; set; }
        /// <summary>产品类型</summary>
        public string? ProductType { get; set; }
        /// <summary>产品编码</summary>
        public string? ProductCode { get; set; }
        /// <summary>产品名称</summary>
        public string? ProductName { get; set; }
        /// <summary>规格型号</summary>
        public string? SpecModel { get; set; }
        /// <summary>单位</summary>
        public string? Unit { get; set; }
        /// <summary>版本标签</summary>
        public string? VersionTag { get; set; }
        /// <summary>图号</summary>
        public string? DrawingNo { get; set; }
        /// <summary>是否默认</summary>
        public bool? IsDefault { get; set; }
        /// <summary>日产量</summary>
        public decimal? DailyOutput { get; set; }
        /// <summary>生效日期</summary>
        public DateTime? EffectiveDate { get; set; }
        /// <summary>失效日期</summary>
        public DateTime? ExpireDate { get; set; }
        /// <summary>备注</summary>
        public string? Remark { get; set; }

        /// <summary>配件明细</summary>
        public List<BomAccessoryDto>? Accessories { get; set; }
        /// <summary>关联的工艺路线 Id 列表</summary>
        public List<Guid>? RouteIds { get; set; }
        /// <summary>关联的工艺路线（含每条的负责人/部门/单位工时量/备注等）</summary>
        public List<BomRouteLinkDto>? ProcessRoutes { get; set; }
        /// <summary>附件列表</summary>
        public List<AttachmentDto>? Attachments { get; set; }

        /// <summary>操作者（用于写操作日志）</summary>
        public string? Operator { get; set; }
    }

    /// <summary>
    /// BOM 配件明细 DTO
    /// </summary>
    public class BomAccessoryDto
    {
        public string? ItemCode { get; set; }
        public string? ItemName { get; set; }
        public string? SpecModel { get; set; }
        public string? Unit { get; set; }
        public string? ItemType { get; set; }
        public string? Category { get; set; }
        public string? Brand { get; set; }
        public decimal? TotalStock { get; set; }
        public decimal? PurchasePrice { get; set; }
        public decimal? SalesPrice { get; set; }
        public decimal? Qty { get; set; }
        public int? SortNo { get; set; }
        public string? Remark { get; set; }
    }

    /// <summary>
    /// 附件 DTO（仅元数据，不含文件本体）
    /// </summary>
    public class AttachmentDto
    {
        public string? FileName { get; set; }
        public string? OriginalFileName { get; set; }
        public long? FileSize { get; set; }
        public string? ContentType { get; set; }
        public string? Url { get; set; }
        public string? Remark { get; set; }
    }

    /// <summary>
    /// BOM 列表查询参数
    /// </summary>
    public class BomQueryDto
    {
        public string? Code { get; set; }
        public string? Subject { get; set; }
        public bool? IsDefault { get; set; }
        public string? Version { get; set; }
        public string? ProductType { get; set; } // FG/SFG/OTH
        public string? ProductName { get; set; }
        public decimal? DailyMin { get; set; }
        public decimal? DailyMax { get; set; }
        public string? Kw { get; set; }
        public int Page { get; set; } = 1;
        public int PageSize { get; set; } = 10;
    }

    /// <summary>
    /// BOM 列表项返回模型（用于列表页展示）
    /// </summary>
    public class BomListItemDto
    {
        public Guid? Id { get; set; }
        public string? Code { get; set; }           // 显示用：优先 BomCode，其次 SystemCode
        public string? Subject { get; set; }        // 这里用 Name 或 ProductName；与前端“BOM主题”对应
        public string? ProductCode { get; set; }
        public string? ProductName { get; set; }
        public string? Model { get; set; }          // 规格型号
        public string? Unit { get; set; }
        public string? Category { get; set; }       // 用 ProductType 代填
        public bool? IsDefault { get; set; }
        public decimal? DailyOutput { get; set; }   // 映射实体 DailyOutput
        public string? Version { get; set; }        // VersionTag
    }

    /// <summary>
    /// BOM 详情页 DTO
    /// </summary>
    public class BomDetailsDto
    {
        public Guid Id { get; set; }
        public string? Code { get; set; }
        public string? Subject { get; set; }
        public bool? IsDefault { get; set; }
        public string? Version { get; set; }
        public string? ProductType { get; set; }
        public string? ProductCode { get; set; }
        public string? ProductName { get; set; }
        public string? SpecModel { get; set; }
        public string? Unit { get; set; }
        public decimal? DailyOutput { get; set; }
        public decimal? CurrentStock { get; set; }
        public decimal? EstimatedCost { get; set; }
        public string? Remark { get; set; }
        public string? CreatedBy { get; set; }
        public DateTime? CreatedAt { get; set; }
        public string? UpdatedBy { get; set; }
        public DateTime? UpdatedAt { get; set; }
        public List<BomAccessoryDetailDto> Accessories { get; set; } = new();
        public List<BomRouteLinkDto> ProcessRoutes { get; set; } = new();
        public List<OperationLogDto> Logs { get; set; } = new();
    }

    /// <summary>
    /// BOM 配件明细 DTO（详情页）
    /// </summary>
    public class BomAccessoryDetailDto
    {
        public int Index { get; set; }
        public string? ItemCode { get; set; }
        public string? ItemName { get; set; }
        public string? SpecModel { get; set; }
        public string? Unit { get; set; }
        public string? Brand { get; set; }
        public decimal? Qty { get; set; }
        public decimal? LossRate { get; set; } // 0-100
        public decimal CurrentStock { get; set; }
        public decimal AvailableProduce { get; set; }
    }

    /// <summary>
    /// BOM 关联的工艺路线（简要信息）
    /// </summary>
    public class BomRouteLinkDto
    {
        public Guid? RouteId { get; set; }
        public string? RouteCode { get; set; }
        public string? RouteName { get; set; }
        public int? Sort { get; set; }
        public DateTime? SelectedAt { get; set; }
        public string? Owner { get; set; }
        public string? Department { get; set; }
        public decimal? UnitWorkHours { get; set; }
        public string? Remark { get; set; }
    }

    /// <summary>
    /// 操作日志 DTO
    /// </summary>
    public class OperationLogDto
    {
        public string? Operator { get; set; }
        public DateTime? OperateTime { get; set; }
        public string? Content { get; set; }
    }

    /// <summary>
    /// BOM 展开结果行 DTO（拍平后的每个用料节点）
    /// </summary>
    public class ExplodedRowDto
    {
        public int Level { get; set; }                 // 层级，从1开始
        public string? ParentItemCode { get; set; }    // 父节点物料编码（根节点为空）
        public string? ItemCode { get; set; }
        public string? ItemName { get; set; }
        public string? SpecModel { get; set; }
        public string? Unit { get; set; }
        public string? Brand { get; set; }
        public decimal? NodeQty { get; set; }          // 当前层用量/单耗
        public decimal? LossRate { get; set; }         // 当前层损耗率（0-100）
        public decimal? AccQty { get; set; }           // 累计用量（根到此节点的乘积，考虑损耗）
        public string? BomCode { get; set; }           // 若该物料有BOM，则返回子BOM编号
        public string? Version { get; set; }           // 子BOM版本
    }
}
